package com.anruisi.hxmes.auth.service.impl;

import com.anruisi.hxmes.auth.entity.SysUser;
import com.anruisi.hxmes.auth.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
*@author cmy
*@date 2020-11-06 13:29:10
*/
@Slf4j
@Service("sysUserService")
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    JdbcTemplate jdbcTemplate;


    @Override
    public SysUser selectUserByName(String username) {
        try {
            return jdbcTemplate.queryForObject("SELECT id,username,password,status,mobile FROM sys_user WHERE username = ?",new BeanPropertyRowMapper<>(SysUser.class),username);
        } catch (DataAccessException e) {
            log.error(e.getMessage());
            return null;
        }
    }

    @Override
    public List<String> selectSysRoleByUserId(Integer userId) {
        return jdbcTemplate.queryForList("SELECT sr.name FROM sys_role sr LEFT JOIN sys_user_role sur ON sr.id = sur.rid WHERE sur.uid = ?",String.class,userId);
    }

}